/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateNewUser.java
 *
 * Created on Mar 29, 2011, 11:29:41 AM
 */

package Project2.cs342.project1.mathNotebook.gui;

import Project2.cs342.project1.mathNotebook.data.*;
import Project2.cs342.project1.mathNotebook.Database.DBUsers;
import java.math.BigInteger;
import javax.swing.*;

/**
 *
 * @author Rick
 */
public class CreateNewUser extends javax.swing.JFrame {

    /** Creates new form CreateNewUser */
    public CreateNewUser() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        userNameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        createNewUserButton = new javax.swing.JButton();
        usernameField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        cancelButton = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Create New User:");

        userNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        userNameLabel.setText("Username:");

        passwordLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        passwordLabel.setText("Password:");

        createNewUserButton.setText("Create New User");
        createNewUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createNewUserButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(createNewUserButton)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(userNameLabel)
                            .addComponent(passwordLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordField)
                            .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userNameLabel)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createNewUserButton)
                    .addComponent(cancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Creates a new User from the information in the username and password textfields.
     *
     */
    private void createNewUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createNewUserButtonActionPerformed
        String newUserName;
        String newUserPassword;
        Boolean validNewUser;
        DBUsers database = new DBUsers();
        User user = new User();
        RSAKeyGenerator rsaKeyGenerator = new RSAKeyGenerator();
        DESPasswordGenerator desPasswordGenerator = new DESPasswordGenerator();


        newUserName = usernameField.getText().trim();
        newUserPassword = passwordField.getText().trim();
        validNewUser = true;
        if(newUserName.equals("") || newUserPassword.equals("")){
            validNewUser = false;
            JOptionPane.showMessageDialog(new JFrame(),"Please enter a username and password","Missing Entry",JOptionPane.ERROR_MESSAGE);
        }
        else{

            if(database.doesUserExist(newUserName)){
                JOptionPane.showMessageDialog(new JFrame(),"This username is already taken.  Please select a new one.","Duplicate Username",JOptionPane.ERROR_MESSAGE);
                validNewUser = false;
            }
        }
        
        if(validNewUser){

            //Generate RSA keys and DES passsword
            rsaKeyGenerator.generateKeys();
            desPasswordGenerator.generateRandomPassword();

            //Create new user in local memory and on database
            user.setUsername(newUserName);
            user.setPassword(newUserPassword);
            user.setDesDefaultKey(desPasswordGenerator.getRandomPassword());
            user.setRsaPublicKey(rsaKeyGenerator.getPublicKey());
            user.setRsaPrivateKey(rsaKeyGenerator.getPrivateKey());



            database.insertIntoUsers(newUserName, newUserPassword);

            //Open math notebook
            UserInterface userInterface = new UserInterface(user);
            userInterface.setVisible(true);

            //Clear text fields and make invisible
            usernameField.setText("");
            passwordField.setText("");
            this.dispose();

        }

    }//GEN-LAST:event_createNewUserButtonActionPerformed

    /**
     * Opens a new Login window and destroys the current CreateNewUser window.
     *
     */
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        Login login = new Login();
        login.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CreateNewUser().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton createNewUserButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel userNameLabel;
    private javax.swing.JTextField usernameField;
    // End of variables declaration//GEN-END:variables

}
